package com.hongdee.base.carconfigure.controller;

import com.hongdee.base.carconfigure.entity.TBcCarConfigure;
import com.hongdee.base.carconfigure.service.ICarConfigureService;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 名  称:TBcCarConfigureController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.base.carconfigure.controller
 * 作  者: 术洪超
 * 日  期:2017-05-18 22:18
 */

@RestController
@RequestMapping("/base/carConfigure")
public class TBcCarConfigureController {

    @Autowired
    private ICarConfigureService iCarConfigureService;

    /**
     * @param data 要保存的实体
     * @return Map
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */

    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TBcCarConfigure data) throws Exception {

        //判断必填项,
        EntityUtils.valid(data);
        TBcCarConfigure cm = this.iCarConfigureService.saveOrUpdate(data, uid);
        ResultEntity res = new ResultEntity();
        res.setData(cm);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 启用配置
     * @author 术洪超
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择配置！");
        }
        this.iCarConfigureService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 启用配置
     * @author 术洪超
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择配置！");
        }
        this.iCarConfigureService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功");
        return res;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 术洪超
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择配置！");
        }
        Map map = iCarConfigureService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }


    /**
     * @param uid     登录用户 ID
     * @param token   登录用户 Token
     * @param start   起始记录
     * @param limit   页面大小
     * @param modelId 车型ID
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "modelId", defaultValue = "") String modelId,
                    @RequestParam(name = "configureCode", defaultValue = "") String configureCode) throws Exception {

        Map map = iCarConfigureService.page(start, limit, modelId, configureCode);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * @param modelId 车型ID
     * @return List
     * @throws Exception
     * @desc 根据车型ID查询配置下拉菜单
     * @author 术洪超
     */
    @RequestMapping("/combo")
    @ResponseBody
    public List<Map> combo(String modelId) throws Exception {
        return this.iCarConfigureService.combo(modelId);
    }

}
